/**
 * 
 */
package kr.co.insoft.publicmesh.controller.configration;

import kr.co.insoft.core.model.configration.cdt.CDTEntityModel;
import kr.co.insoft.core.model.paging.PagingModel;
import kr.co.insoft.core.service.configration.cdt.CDTService;
import kr.co.insoft.framework.exceptions.ServiceException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author IN-SOFT Inc. GoodwillDD(sylee@in-soft.co.kr)
 * 
 */
@Controller
@RequestMapping(value = "configration/cdt")
public class CDTController {
	final String TILES_PAGE_PREFIX = "tiles/configration/cdt";
	final String SINGLE_PAGE_PREFIX = "configration/cdt";

	@Autowired
	CDTService cdtService;

	@RequestMapping(value = "lists")
	public String lists(
			Model model,
			@ModelAttribute("pagingModel") PagingModel pagingModel,
			@RequestParam(value = "ip", defaultValue = "N", required = false) String ip)
			throws Exception {
		model.addAttribute("pagingModel", cdtService.getLists(pagingModel));
		if (ip.equals("Y"))
			return SINGLE_PAGE_PREFIX + "/lists";

		return TILES_PAGE_PREFIX + "/lists";
	}

	@RequestMapping(value = "createForm", method = RequestMethod.GET)
	public String createForm(
			Model model,
			@RequestParam(value = "ip", defaultValue = "N", required = false) String ip) {
		model.addAttribute("cDTEntityModel", new CDTEntityModel());
		if (ip.equals("Y"))
			return SINGLE_PAGE_PREFIX + "/createForm";

		return TILES_PAGE_PREFIX + "/createForm";
	}

	@RequestMapping(value = "createForm", method = RequestMethod.POST)
	public String create(
			@ModelAttribute("cDTEntityModel") CDTEntityModel cdtEntityModel,
			@RequestParam(value = "ip", defaultValue = "N", required = false) String ip,
			final BindingResult bindingResult) throws Exception {
		if (bindingResult.hasErrors()) {
			if (ip.equals("Y"))
				return SINGLE_PAGE_PREFIX + "/createForm";

			return TILES_PAGE_PREFIX + "/createForm";
		}

		cdtService.createCDT(cdtEntityModel);

		if (ip.equals("Y"))
			return "redirect:./lists?ip=Y";

		return "redirect:./lists";
	}

	@RequestMapping(value = "view", method = RequestMethod.GET)
	public String view(
			Model model,
			@RequestParam("userTypeId") String userTypeId,
			@RequestParam(value = "ip", defaultValue = "N", required = false) String ip)
			throws Exception {
		model.addAttribute("cDTEntityModel", cdtService.getCDT(userTypeId));
		if (ip.equals("Y"))
			return SINGLE_PAGE_PREFIX + "/view";

		return TILES_PAGE_PREFIX + "/view";
	}

	@RequestMapping(value = "edit", method = RequestMethod.POST)
	public ResponseEntity<Boolean> edit(
			@ModelAttribute("cDTEntityModel") CDTEntityModel cdtEntityModel)
			throws Exception {
		try {
			cdtService.editCDT(cdtEntityModel);
			return new ResponseEntity<Boolean>(true, HttpStatus.OK);
		} catch (ServiceException e) {
			return new ResponseEntity<Boolean>(false, HttpStatus.OK);
		}
	}

	@RequestMapping(value = "remove", method = RequestMethod.POST)
	public @ResponseBody ResponseEntity<Boolean> remove(
			@ModelAttribute("cDTEntityModel") CDTEntityModel cdtEntityModel) {
		try {
			cdtService.removeCDT(cdtEntityModel);
			return new ResponseEntity<Boolean>(true, HttpStatus.OK);
		} catch (ServiceException e) {
			return new ResponseEntity<Boolean>(false, HttpStatus.OK);
		}
	}
}
